package axio.sdf.read;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Reader {

	public int readFile(String path) {

		BufferedReader br = null;
		int count = 0;

		try {

			String sCurrentLine;

			br = new BufferedReader(new FileReader(path));

			while ((sCurrentLine = br.readLine()) != null) {
				if (sCurrentLine.equals("$$$$")) {
					count++;
				}

			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return count;

	}
	
	public void writeFile(int index_1, String filePath, String fileName, StringBuffer write_string, int i) throws IOException{
		
		File file;
		if (index_1 != -1) {
			file = new File(filePath + "\\" + fileName + "_division-" + i + ".sdf");
		} else {
			file = new File(filePath + "/" + fileName + "_division-" + i + ".sdf");
		}

		if (!file.exists()) {
			file.createNewFile();
		}
		
		FileWriter fileWritter = new FileWriter(file, true);
		BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
		bufferWritter.write(write_string.toString());
		bufferWritter.close();
		//System.out.println( fileName + "_division-" + i + " Complete");
		
		
	}

	public void CountDivideFile(String path, int dividenumber) {

		BufferedReader br = null;

		try {
			
			int i = 1;
			int pre_i = 0;
			int files_for_one = 0;
			int remaining = 0;
			int loop_count=0;
			int index_1;
			int count;

			String sCurrentLine;
			String filePath;
			String fileName;
			StringBuffer write_string = new StringBuffer();
			
			br = new BufferedReader(new FileReader(path));
			index_1 = path.lastIndexOf("\\");
			
			count= readFile(path);
		
			if(count % dividenumber==0){
				files_for_one = count/dividenumber;
			}else{
				remaining = count % dividenumber;
				files_for_one = count/dividenumber;
			}

			if (index_1 != -1) {
				filePath = path.substring(0, index_1);
				fileName = path.substring(index_1 + 1, path.lastIndexOf("."));
			} else {
				int index_2 = path.lastIndexOf("/");
				filePath = path.substring(0, index_2);
				fileName = path.substring(index_2 + 1, path.lastIndexOf("."));
			}
			
			System.out.println("\nS t a r t e d  t o  D i v i d e . . . ");
			
			while ((sCurrentLine = br.readLine()) != null) {
				
				if (sCurrentLine.equals("$$$$")) {
					loop_count++;
					write_string.append("> <DIT_AXIO_ID>\nDIT_AXIO_ID_0000"+loop_count+"\n\n");
					write_string.append(sCurrentLine).append("\r\n");
					
					
					if((loop_count==pre_i+files_for_one) && remaining ==0){
						pre_i=loop_count;
						writeFile(index_1, filePath, fileName, write_string, i);
						write_string = new StringBuffer();
						i++;
						
					}else if((loop_count==pre_i+files_for_one+1) && remaining !=0){
						remaining--;
						pre_i=loop_count;
						writeFile(index_1, filePath, fileName, write_string, i);
						write_string = new StringBuffer();
						i++;
					}
				
					if (i > dividenumber) {
						i = 1;
					}
				}else{
					write_string.append(sCurrentLine).append("\r\n");
				}
				
				sCurrentLine = null;
			}
			
			System.out.println("\nD i v i d e d  t o  "+dividenumber+"  p a r t s . . . \n\n");

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
		

	}
	
	
}